Image 최적화

✒️ 2025-05-28 11:06 내용 수정



Image Component

  1. local 이미지 : local에 저장된 이미지를 사용하려면 이미지를 public/ 폴더에 저장해야 하며, jpg, png, webp 확장자 파일을 사용할 수 있다.
    • src는 public 폴더 내에서의 파일 경로를 작성한다.
    • 이미지의 크기는 원본 이미지 파일의 크기를 사용하여 자동으로 적용된다.
import Image from "next/image"

export default function Test() {
	<Image src="/images/test.png" alt="이미지"
		// width={100} // local은 자동으로 원본 이미지의 너비로 지정함
		// height={100} // local은 자동으로 원본 이미지의 높이로 지정함
	/>
}
  1. 원격 서버 이미지 : 원격 서버에 있는 이미지를 사용하려면 src는 URL string이어야 한다.
    • Next.js는 빌드 프로세스 중에 원격 파일에 접속할 수 없기 때문에 width, height 등의 옵션을 반드시 설정해야 한다.
import Image from "next/image"

export default function Test() {
	<Image src="https://test.com/public/test.png" alt="이미지"
		width={100} // 원격 서버 이미지는 반드시 크기를 지정해야 함
		height={100}
	/>
}
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 's3.amazonaws.com',
        port: '',
        pathname: '/test/**'
      }
    ]
  }
};

export default nextConfig;
import Image from "next/image"

export default function Test() {
    return(
	    {/* 부모 요소인 .box에 스타일을 지정해준다. */}
		<div className="box">
			<Image src="/images/product-08.png" // public/images/product-08.png
				fill // 부모 요소에 맞게 채움
				alt="상품이미지"
			/>
		</div>
}

Image Component 예제

import Image from "next/image"

export default function Test() {
    return(
        <>
            {/* 원본 사이즈, 원본 확장자대로 출력됨 */}
            <img src="/images/product-08.png" // public/images/product-08.png
                width={120}
                height={120}
                alt="상품이미지"
            />
            {/* width, height를 필수로 지정, 또는 fill을 사용  */}
            <Image src="/images/product-08.png" // public/images/product-08.png
                width={120}
                height={120}
                alt="상품이미지"
            />
        </>
    )
}

next image component 1.png

next image component 2.png
next image component 3.png